package com.ehotting.edsta.csharpServer.websocket;

import com.alibaba.fastjson.JSON;
import com.ehotting.eaf.redis.domain.RedisMsgDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RedisMessageListener implements MessageListener {

    private WebSocketController webSocketController;

    public void setWebSocketController(WebSocketController webSocketController) {
        this.webSocketController = webSocketController;
    }

    @Override
    public void onMessage(Message message, byte[] bytes) {
        String msg = new String(message.getBody());
        log.info("received message from redis publish, msg is {}",msg);
        RedisMsgDto<?> redisMsgDto = JSON.parseObject(msg, RedisMsgDto.class);
        if(webSocketController!=null){
            try {
                webSocketController.sendMessage(redisMsgDto);
            }catch (Exception e){
                log.error("RedisSubListener消息订阅监听异常：" + e.getMessage());
            }
        }
    }
}